<?php
/**
 * As published in http://www.julianstricker.com/validate-size-uploaded-image-symfony-1314
 * only the messages has been edited
*/
class jsValidatorImage extends sfValidatorFile
{
  protected function configure($options = array(), $messages = array())
  {
    parent::configure($options, $messages);

    $this->addOption('minx', false);
    $this->addOption('miny', false);
    $this->addOption('maxx', false);
    $this->addOption('maxy', false);

    $this->addMessage('not_a_image', 'El archivo no es una imagen.');
    $this->addMessage('minx', 'La imagen debe tener mínimo %minx% pixel de ancho.');
    $this->addMessage('maxx', 'La imagen debe tener máximo %maxx% pixel de ancho.');
    $this->addMessage('miny', 'La imagen debe tener mínimo %miny% pixel de alto.');
    $this->addMessage('maxy', 'La imagen debe tener máximo %maxy% pixel de alto.');

  }


  protected function doClean($value)
  {
    $validatedFile = parent::doClean($value);

    // check image size

    $imagedata=getimagesize($value['tmp_name']);

    if (!$imagedata){
      throw new sfValidatorError($this, 'not_a_image');
    }
    if ($this->hasOption('minx') && $this->getOption('minx')!==false && $imagedata[0] < $this->getOption('minx'))
    {
      throw new sfValidatorError($this, 'minx', array('minx' => $this->getOption('minx'), 'size' => (int) $imagedata[0]));
    }
    if ($this->hasOption('maxx') && $this->getOption('maxx')!==false && $imagedata[0] > $this->getOption('maxx'))
    {
      throw new sfValidatorError($this, 'maxx', array('maxx' => $this->getOption('maxx'), 'size' => (int) $imagedata[0]));
    }
    if ($this->hasOption('miny') && $this->getOption('miny')!==false && $imagedata[1] < $this->getOption('miny'))
    {
      throw new sfValidatorError($this, 'miny', array('miny' => $this->getOption('miny'), 'size' => (int) $imagedata[1]));
    }
    if ($this->hasOption('maxy') && $this->getOption('maxy')!==false && $imagedata[1] > $this->getOption('maxy'))
    {
      throw new sfValidatorError($this, 'maxy', array('maxy' => $this->getOption('maxy'), 'size' => (int) $imagedata[1]));
    }

    return $validatedFile;
  }
}

